package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

// @title    CreateInWarehouseInfo
// @description   create a InWarehouseInfo
// @param     InWarehouse               model.InWarehouseInfo
// @auth                     （2020/04/05  20:22）
// @return    err             error

func CreateInWarehouseInfo(InWarehouse model.InWarehouseInfo) (err error) {
	err = global.GVA_DB.Create(&InWarehouse).Error
	return err
}

// @title    DeleteInWarehouseInfo
// @description   delete a InWarehouseInfo
// @auth                     （2020/04/05  20:22）
// @param     InWarehouse               model.InWarehouseInfo
// @return                    error

func DeleteInWarehouseInfo(InWarehouse model.InWarehouseInfo) (err error) {
	err = global.GVA_DB.Delete(InWarehouse).Error
	return err
}

// @title    DeleteInWarehouseInfoByIds
// @description   delete InWarehouseInfos
// @auth                     （2020/04/05  20:22）
// @param     InWarehouse               model.InWarehouseInfo
// @return                    error

func DeleteInWarehouseInfoByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.InWarehouseInfo{},"id in ?",ids.Ids).Error
	return err
}

// @title    UpdateInWarehouseInfo
// @description   update a InWarehouseInfo
// @param     InWarehouse          *model.InWarehouseInfo
// @auth                     （2020/04/05  20:22）
// @return                    error

func UpdateInWarehouseInfo(InWarehouse *model.InWarehouseInfo) (err error) {
	err = global.GVA_DB.Save(InWarehouse).Error
	return err
}

// @title    GetInWarehouseInfo
// @description   get the info of a InWarehouseInfo
// @auth                     （2020/04/05  20:22）
// @param     id              uint
// @return                    error
// @return    InWarehouseInfo        InWarehouseInfo

func GetInWarehouseInfo(id uint) (err error, InWarehouse model.InWarehouseInfo) {
	err = global.GVA_DB.Where("id = ?", id).First(&InWarehouse).Error
	return
}

// @title    GetInWarehouseInfoInfoList
// @description   get InWarehouseInfo list by pagination, 分页获取InWarehouseInfo
// @auth                     （2020/04/05  20:22）
// @param     info            PageInfo
// @return                    error

func GetInWarehouseInfoInfoList(info request.InWarehouseInfoSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.InWarehouseInfo{})
    var InWarehouses []model.InWarehouseInfo
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.ModelName != "" {
        db = db.Where("`model_name` LIKE ?","%"+ info.ModelName+"%")
    }
    if info.CheckStd != "" {
        db = db.Where("`check_std` LIKE ?","%"+ info.CheckStd+"%")
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&InWarehouses).Error
	return err, InWarehouses, total
}